<?php
class Gallery_Model_Image extends Promotor_Model_Abstract {
	
	const UPLOAD_PATH  = 'upload/gallery/';
	
	protected $_dbTableClass = 'Gallery_Model_DbTable_Image';
	
	protected $_cachedMethods = array(
		'fetchRand',
		'fetchAll'
	);
	
	/**
	 * @return Zend_Db_Select
	 */
	public function selectListGrid() {
		$table = $this->getDbTable();
		$select = new Zend_Db_Select($table->getAdapter());
		$select->from(array('gi' => 'gallery_image'))
			   ->joinLeft(array('ga'=>'gallery_album'),'ga.id = gi.album_id', array('album_name' => 'name'));
		return $select;
	}
	
	/**
	 * @return array
	 */
	public function fetchAll() {
		$table = $this->getDbTable();
		try {
			$rowset = $table->fetchAll();
			$this->_setStatus(self::SUCCESS);
			return $rowset->toArray();
		} catch (Zend_Db_Table_Exception $e) {
			$this->_setStatus(self::FAILURE);
			$this->_addException($e);
		}
	}

	/**
	 * @param integer $limit
	 * @return array
	 */
	public function fetchRand($limit = null) {
		if (!is_numeric($limit) || $limit < 1) {
			$limit = 1;
		} else {
			$limit = abs((int) $limit);
		}

		$table = $this->getDbTable();
		$select = $table->select()->order('RAND()')->limit($limit);
		try {
			$rowset = $table->fetchAll($select);
			$this->_setStatus(self::SUCCESS);
			return $rowset->toArray();
		} catch (Zend_Db_Table_Exception $e) {
			$this->_setStatus(self::FAILURE);
			$this->_addException($e);
		}
	}
	
	/**
	 * @param integer $id
	 * @return array
	 */
	public function findById($id) {
		$table = $this->getDbTable();
		$where = $table->select()
					   ->where('id = ?', $id)
					   ->where('publicated = 1');
		try {
			$row = $table->fetchRow($where);
			if(!$row instanceof Zend_Db_Table_Row_abstract) {
				$this->_setStatus(self::FAILURE);
				return;
			}
			$this->_setStatus(self::SUCCESS);
			return $row->toArray();
		} catch (Zend_Db_Table_Exception $e) {
			$this->_setStatus(self::FAILURE);
			$this->_addException($e);
		}
	}
	
	/**
	 * @param array $data
	 * @return void
	 */
	public function editableUpdate(array $data) {
		$table = $this->getDbTable();
		$db = $table->getAdapter();

		$parentKey = $table->info(Zend_Db_Table::PRIMARY);
		$whereBase = implode(' = %s AND ', $parentKey) . ' = %s';
		
		$db->beginTransaction();
		try {
			foreach ($data as $key => $values) {
				$key = explode(KontorX_DataGrid_Cell_Editable_Abstract::SEPARATOR, $key);
				$where = vsprintf($whereBase, $key);
				$table->update($values, $where);
			}
			$db->commit();
			
			// notify observers
			$manager = Promotor_Observable_Manager::getInstance();
			$list = $manager->notify('gallery_image_editable_update_post');
			$this->_addMessages($list->getMessages());
			
			$this->_setStatus(self::SUCCESS);
		} catch (Zend_Db_Table_Exception $e) {
			$db->rollBack();
			$this->_setStatus(self::FAILURE);
			$this->_addException($e);
		}
	}
	
	/**
	 * @param array $data
	 * @param integer $albumId
	 * @return void
	 */
	public function editableUpdateMove(array $data, $albumId) {
		$table = $this->getDbTable();
		$db = $table->getAdapter();

		$parentKey = $table->info(Zend_Db_Table::PRIMARY);
		$whereBase = implode(' = %s AND ', $parentKey) . ' = %s';

		$db->beginTransaction();
		try {
			foreach ($data as $key => $values) {
				// większe od zera .. zakładam że autoincrement jest od 1..
				if (isset($values['id']) && $values['id'] > 0) {
					// TODO Nalezy pamiętać o tym że mogą to być rekordy z tree..
					$key = explode(KontorX_DataGrid_Cell_Editable_Abstract::SEPARATOR, $key);
					$where = vsprintf($whereBase, $key);
					$table->update(array('album_id' => $albumId),$where);
				}
			}
			$db->commit();
			
			// notify observers
			$manager = Promotor_Observable_Manager::getInstance();
			$list = $manager->notify('gallery_image_editable_move_post');
			$this->_addMessages($list->getMessages());
			
			$this->_setStatus(self::SUCCESS);
		} catch (Zend_Db_Table_Exception $e) {
			$db->rollBack();
			$this->_setStatus(self::FAILURE);
			$this->_addException($e);
		}
	}
	
	/**
	 * @param array $data
	 * @return void
	 */
	public function editableDelete(array $data) {
		$table = $this->getDbTable();
		$db = $table->getAdapter();

		$parentKey = $table->info(Zend_Db_Table::PRIMARY);
		$whereBase = implode(' = %s AND ', $parentKey) . ' = %s';
		
		$db->beginTransaction();
		try {
			foreach ($data as $key => $values) {
				// większe od zera .. zakładam że autoincrement jest od 1..
				if (isset($values['id']) && $values['id'] > 0) {
					// TODO Nalezy pamiętać o tym że mogą to być rekordy z tree..
					$key = explode(KontorX_DataGrid_Cell_Editable_Abstract::SEPARATOR, $key);
					$where = vsprintf($whereBase, $key);
					$table->delete($where);
				}
			}
			$db->commit();
			
			// notify observers
			$manager = Promotor_Observable_Manager::getInstance();
			$list = $manager->notify('gallery_image_editable_delete_post');
			$this->_addMessages($list->getMessages());
			
			$this->_setStatus(self::SUCCESS);
		} catch (Zend_Db_Table_Exception $e) {
			$db->rollBack();
			$this->_setStatus(self::FAILURE);
			$this->_addException($e);
		}
	}
}